package bancosys.tec.utils.formatter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Classe utilitária para formatação de strings.
 * 
 * @author piercio
 */
public final class StringFormatter {

    private static final Pattern PATTERN_NUMERO = Pattern.compile("[0-9]");

    private static final Pattern PATTERN_A = Pattern.compile("[àãÀÃâÂáÁ]");

    private static final Pattern PATTERN_E = Pattern.compile("[èÈêÊéÉ]");

    private static final Pattern PATTERN_I = Pattern.compile("[ìÌîÎíÍ]");

    private static final Pattern PATTERN_O = Pattern.compile("[òÒõÕôÔóÓ]");

    private static final Pattern PATTERN_U = Pattern.compile("[ùÙûÛúÚ]");

    private static final Pattern PATTERN_C = Pattern.compile("[çÇ]");

    private static final Pattern PATTERN_SPACE = Pattern.compile("[^a-z0-9]");

    /**
     * Contrutor
     */
    private StringFormatter() {
        // vazio
    }

    /**
     * Substitui todos os caracteres acentuados por caracteres sem acento, cedilhas por c e espaços por _ da palavra original.
     * 
     * @param original a palavra original
     * @return idGerado a palavra sem caracteres especiais
     */
    public static String replaceSpecialCharacters(String original) {
        Matcher matcher = null;
        String idGerado = original.toLowerCase();

        matcher = PATTERN_NUMERO.matcher(idGerado);
        if (matcher.find(0)) {
            idGerado = "_" + idGerado;
        }

        matcher = PATTERN_A.matcher(idGerado);
        idGerado = matcher.replaceAll("a");

        matcher = PATTERN_E.matcher(idGerado);
        idGerado = matcher.replaceAll("e");

        matcher = PATTERN_I.matcher(idGerado);
        idGerado = matcher.replaceAll("i");

        matcher = PATTERN_O.matcher(idGerado);
        idGerado = matcher.replaceAll("o");

        matcher = PATTERN_U.matcher(idGerado);
        idGerado = matcher.replaceAll("u");

        matcher = PATTERN_C.matcher(idGerado);
        idGerado = matcher.replaceAll("c");

        matcher = PATTERN_SPACE.matcher(idGerado);
        idGerado = matcher.replaceAll("_");

        return idGerado;
    }
}
